package org.hehui.supervisorysystem.rcpcore;


import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.hehui.supervisorysystem.login.Login;
import org.hehui.supervisorysystem.ui.SysTray;
import org.hehui.supervisorysystem.util.CacheImage;
import org.hehui.supervisorysystem.util.IAppConstants;
import org.hehui.supervisorysystem.util.IImageKey;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	private SysTray sysTray;
	
	
    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }

    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    public void preWindowOpen() {
    	//登陆验证
		Login login = new Login();
		if (!login.haveLoggedin()) {
			//Platform.endSplash();
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().close();
		}
    	
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        //初始窗口大小
        //configurer.setInitialSize(new Point(700, 550));
        
        //显示工具栏
        //configurer.setShowMenuBar(true);
        //configurer.setShowCoolBar(true);
        //显示状态栏
        configurer.setShowStatusLine(true);
        //显示状态栏进度指示器
        configurer.setShowProgressIndicator(true);
        //显示选择透视图的工具栏图标
        configurer.setShowPerspectiveBar(true);
        PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR, IWorkbenchPreferenceConstants.TOP_RIGHT);
        //PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR, false); 
        PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS, "org.hehui.supervisorysystem.perspective,org.hehui.supervisorysystem.ui.perspective.Messenger");
     
        //显示快速打开视图工具栏
        configurer.setShowFastViewBars(true);
        
        PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.SHOW_SYSTEM_JOBS, true);
        PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR, true);
        PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, true);

        
        //窗口标题
        configurer.setTitle(IAppConstants.APPLICATION_TITLE);
        //窗口样式
        //configurer.setShellStyle(SWT.MIN|SWT.CLOSE);
        
        
    }

	@Override
	public void postWindowOpen() {
		//窗口居中
		Shell shell = getWindowConfigurer().getWindow().getShell();
		Rectangle screenSize = Display.getDefault().getClientArea();
		Rectangle frameSize = shell.getBounds();
		shell.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);

		//创建托盘
		createSystemTray();
		
		//设置状态栏临时消息
		IStatusLineManager statusLineManager = getWindowConfigurer().getActionBarConfigurer().getStatusLineManager();
		statusLineManager.setMessage(CacheImage.getINSTANCE().getImage(IAppConstants.APPLICATION_ID, IImageKey.AUTHOR), "Powered By 贺辉");
		
		//参数启动
		String[] args = Platform.getApplicationArgs();
		if(args.length == 1 && args[0].equals("system")){
			getWindowConfigurer().getWindow().getShell().setMinimized(true);
		}
		
		//窗口最大化
		//getWindowConfigurer().getWindow().getShell().setMaximized(true);
		
		
	}
    
	private void createSystemTray(){
		sysTray = new SysTray();
		sysTray.createSysTray(getWindowConfigurer().getWindow());
	}



	@Override
	public boolean preWindowShellClose() {
		sysTray.windowMinimized(getWindowConfigurer().getWindow().getShell());
		return false;
	}
    
	@Override
	public void dispose() {
		sysTray.Dispose();
		CacheImage.getINSTANCE().dispose();
	}
    
	
}
